//
//  ConditionViewController.m
//  LeDiao
//
//  Created by admin on 16/2/29.
//  Copyright © 2016年 xuxuan. All rights reserved.
//

//ViewControllers
#import "GroupViewController.h"
#import "ChineseString.h"
#import "FriendCell.h"
#import "UIImageView+WebCache.h" 


@interface GroupViewController ()
@end

@implementation GroupViewController
{
    AFHTTPRequestOperationManager *_manager;
    NSDictionary *myGroup;
}
@synthesize indexArray;
@synthesize LetterResultArr;

- (void)viewDidLoad
{
    self.title = @"我的群组";
    [super viewDidLoad];

    _tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 120);
    _tableView.sectionIndexColor = GreenColor;
    _tableView.tintColor = GreenColor;
    
   

}
- (void)viewWillAppear:(BOOL)animated{
    [ super viewWillAppear:animated];
    
    if (_nilDic || _nilArray) {
        _nilArray = nil;
        _nilDic = nil;
        myGroup = nil;
    }
    _nilArray = [[NSMutableArray alloc] init];
    _nilDic =[[NSMutableDictionary  alloc] init];

    [self getGroups];

}
#pragma mark - 读取数据库，获取群组列表
- (void)getGroups{
    
    NSArray *stringsToSort=[NSArray arrayWithObjects:
                            @"￥hhh, .$",@" ￥Chin ese ",@"开源中国 ",@"www.oschina.net",
                            @"开源技术",@"社区",@"开发者",@"传播",
                            @"2014",@"2013",@"100",@"中国",@"暑假作业",
                            @"键盘", @"鼠标",@"hello",@"world",
                            nil];
    self.indexArray = [ChineseString IndexArray:stringsToSort];
    self.LetterResultArr = [ChineseString LetterSortArray:stringsToSort];
    
    
    [_tableView reloadData];
    
}



/** 
 * 群组数据在登陆的时候被保存进数据库，该网络请求被弃用
 *
-(void)initData{
    _nilArray = [[NSMutableArray alloc] init];
    _nilDic =[[NSMutableDictionary  alloc] init];
    _manager = [AFHTTPRequestOperationManager manager];
    NSString *url=[NSString stringWithFormat:@"%@group/filterGroup",BaseURL];
    
    NSDictionary *parameters = @{@"memberid": Getmemberid};
    AFHTTPRequestOperation*op= [_manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError*error;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        //NSLog(@"%@",dic);

         NSString *result = dic[@"code"];
     
        
        if ([result isEqualToString:@"4000"]){
            NSData *jsonData = [[dic valueForKey:@"obj"] dataUsingEncoding:NSUTF8StringEncoding];
            NSError *err;
            NSArray *array  = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
            for (NSDictionary *info in array) {
//                Account *a = [[Account alloc]init];
//                [a setMemberUsername:info[@"member_name"]];
                [_nilDic setObject:info forKey:[NSString stringWithFormat:@"%@%@",info[@"group_name"],info[@"id"]]];
                [_nilArray addObject:[NSString stringWithFormat:@"%@%@",info[@"group_name"],info[@"id"]]];
                
            }
//            NSArray *stringsToSort = [_nilArray copy];
            //NSLog(@"%@",dic);
                    NSArray *stringsToSort=[NSArray arrayWithObjects:
                                            @"￥hhh, .$",@" ￥Chin ese ",@"开源中国 ",@"www.oschina.net",
                                            @"开源技术",@"社区",@"开发者",@"传播",
                                            @"2014",@"2013",@"100",@"中国",@"暑假作业",
                                            @"键盘", @"鼠标",@"hello",@"world",
                                            nil];
            
            self.indexArray = [ChineseString IndexArray:stringsToSort];
            self.LetterResultArr = [ChineseString LetterSortArray:stringsToSort];
            
            [_tableView reloadData];
        }
      
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败，error = %@",error.localizedDescription);
        [[[UIAlertView alloc] initWithTitle: @"网络异常" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }];
    op.responseSerializer = [AFCompoundResponseSerializer serializer];
    [op start];
}
*/
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark -Section的Header的值
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [indexArray objectAtIndex:section];
    return key;
}
#pragma mark - Section header view
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
 
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15.0)];
    customView.backgroundColor=BackGray;
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
 //   headerLabel.backgroundColor = [UIColor grayColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = GreenColor;
//    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:14];
    headerLabel.frame = CGRectMake(5, 0, kScreenWidth, 15);
    
    headerLabel.text = [indexArray objectAtIndex:section];
    [customView addSubview:headerLabel];
    
    return customView;
}
//别忘了设置高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15.0;
}

#pragma mark - row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0;
}

#pragma mark -
#pragma mark Table View Data Source Methods
#pragma mark -设置右方表格的索引数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return indexArray;
}
#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSLog(@"title===%@",title);
    return index;
}

#pragma mark -允许数据源告知必须加载到Table View中的表的Section数。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [indexArray count];
}
#pragma mark -设置表格的行数为数组的元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.LetterResultArr objectAtIndex:section] count];
}
#pragma mark -每一行的内容为数组相应索引的值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendCell *cell = [FriendCell createWithTableView:tableView];
    NSString *name =[[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    NSDictionary *d =[_nilDic valueForKey:name];
    cell.name.text = [d valueForKey:@"groupname"];
    NSString *headurl =[d valueForKey:@"grouphead"];
    
    if (![headurl isEqual:[NSNull null]]&&headurl!=nil&&![headurl isEqualToString:@""]) {
        NSString * h = [NSString stringWithFormat:@"%@%@",BaseImageURL,headurl];
        //                    [_head setImage:[Util getImageFromURL:headurl]];
        [cell.headView sd_setImageWithURL:[NSURL URLWithString:h]];
       // cell.headView.userInteractionEnabled = YES;
        if (cell.headView.image==nil) {
            [cell.headView setImage:[UIImage imageNamed:@"head_pic"]];
            
        }
        
    }
    
    return cell;
}
#pragma mark - Select内容为数组相应索引的值
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"---->%@",[[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]);
    
    NSString *str = [[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    NSLog(@"---->%@",str);
    
    NSDictionary *dic = _nilDic[str];
     
    
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
//                                                    message:[[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]
//                                                   delegate:nil
//                                          cancelButtonTitle:@"YES" otherButtonTitles:nil];
//    [alert show];
}

@end
